package cn.yuemouren.stack;

import cn.yuemouren.array.Array;

/**
 * @Author: Timi
 * @Description: 用数组实现栈(运用自己封装的数组工具类)
 * @Date: 2020/5/9 9:12
 * @Version: v1.0
 */
public class ArrayStack<T> implements Stack<T>{

    private Array<T> array;

    public ArrayStack() {
        array = new Array();
    }

    public ArrayStack(int capacity) {
        array = new Array(capacity);
    }

    /**
     * 像栈顶添加元素
     * @param t
     */
    @Override
    public void push(T t) {
        array.addLast(t);
    }

    /**
     * 将栈顶元素弹出栈
     * @return
     */
    @Override
    public T pop() {
        return array.removeLast();
    }

    /**
     * 获取栈顶元素
     * @return
     */
    @Override
    public T peek() {
        return array.getLast();
    }

    /**
     * 获取存储多少值
     * @return
     */
    @Override
    public int getSize() {
        return array.getSize();
    }

    /**
     * 获取栈是否为空
     * @return
     */
    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    /**
     * 获取栈的容量
     * @return
     */
    public int getCapacity(){
        return array.getCapacity();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("ArrayStack: [");
        for (int i = 0; i < array.getSize(); i++){
            sb.append(array.get(i));
            if(i != array.getSize()-1){
                sb.append(", ");
            }
        }
        sb.append("] top");
        return sb.toString();
    }
}
